//运维电子合同列表

interface IoperationEContractListViewModel extends IBaseViewModel<IEContractQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<EContractListResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 查看详情事件
     */
    showContractDetail: (electronicContractNumber: string, agreementContractNumber: string) => void;
    /**
     * 下载电子合同
     */
    downloadContract: (carrierOrderId: string) => void;
    /**
     * 下载补充协议
     */
    downloadAgreement: (carrierOrderId: string) => void;
    /**
     * 跳转下载记录
     */
    skipDownload: () => void;
    /**
     * 列表高度
     */
    heightA: number;
    /**
     * 承运单id
     */
    carrierOrderId: string;
    /**
     * 序号
     */
    index: number;
    /**
     * 合同号
     */
    contractNumber: string;
    /**
     * 客户单位
     */
    clientName: string;
    /**
     * 发货地址
     */
    origin: string;
    /**
     * 送货地址
     */
    destination: string;
    /**
     * 发货时间
     */
    deliveryTime: string;
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 客服专员
     */
    customerName: string;
    /**
     * 合同状态
     */
    eContractStatus: string;
    /**
     * 补充协议状态
     */
    agreementEContractStatus: string;
    /**
     * 合同订立日期
     */
    contractTime: string;

    /**
     * 电子合同第一张图
     */
    contractImgA: string;
    /**
     * 电子合同第二张图
     */
    contractImgB: string;
    /**
     * 电子合同第三张图
     */
    contractImgC: string;
    /**
     * 电子合同类型
     */
    eContractType: string;

    /**
    * 电子合同编号
    */
    electronicContractNumber: string;
    /**
     * 电子合同状态
     */
    electronicContractStatus: string;
    /**
     * 补充协议合同编号
     */
    agreementContractNumber: string;
    /**
     * 补充协议合同状态
     */
    agreementContractStatus: string;
    /**
     * 补充协议第一张图
     */
    agreementImgA: string;
    /**
     * 补充协议第二张图
     */
    agreementImgB: string;
    /**
     * 补充协议第三张图
     */
    agreementImgC: string;


    /**
     * 客服专员下拉
     */
    customerServiceDropDown: EmployeeListItemResponse[];

    /**
    * 合同状态下拉
    */
    econtractStatusDropDown: ValueListItemResponse[];
    /**
     * 是否显示电子合同
     */
    isContractO: boolean;
    /**
     * 是否显示补充协议
     */
    isAgreementO: boolean;
    /**
     * 点击显示电子合同
     */
    ContractO: () => void;
    /**
     * 点击显示补充协议
     */
    AgreementO: () => void;
    /**
     * 是否显示切换补充协议TAB
     */
    isAgreementF: boolean;
    /**
     * 是否显示补充协议下载按钮
     */
    isDownloadA: boolean;
    /**
     * 当前用户真实姓名
     */
    operatorName: string
}

interface IEContractQueryTerms {
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 合同编号
     */
    contractNumber: string;
    /**
     * 车牌号
     */
    carCodeBox: string;
    /**
     * 客服编号
     */
    customerServiceId: string;
    /**
     * 起始地
     */
    origin: string;
    /**
     * 目的地     
     */
    destination: string;
    /**
     * 发货开始时间
     */
    deliveryBegin: string;
    /**
     * 发货结束时间
     */
    deliveryEnd: string;
    /**
     * 合同状态
     */
    econtractStatus: string;
    /**
     * 补充协议状态
     */
    agreementEContractStatus: string;
    /**
     * 合同订立起始时间
     */
    contractStartTime: string;
    /**
     * 合同订立结束时间
     */
    contractEndTime: string;
}


class IEContractListController extends ListController<IoperationEContractListViewModel, IEContractQueryTerms>{
    gridApi: uiGrid.IGridOptionsOf<EContractListResponse>;
    // orderBy: number = 0;
    // asc: boolean = true;
    /**
    * 承运单id-全局
    */
    carrierOrderId: string;
    /**
    * 电子合同编号-全局
    */
    electronicContractNumber: string;
    /**
    * 补充协议编号-全局
    */
    agreementContractNumber: string;
    /**
     * 单选
     */
    selectData: EContractListResponse;
    skip: number = 0;

    constructor($scope: IoperationEContractListViewModel, private valueService: IValueService, private employeeService: IEmployeeService,
        private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private routeService: routeService, private operationService: IOperationService,
        private $location: ng.ILocationService, private orderService: IorderService) {
        super($scope);
        this.init();
    }

    /**
     * 初始化
     */
    init(): void {
        this.$scope.operatorName = JSON.parse(window.localStorage.getItem("loginData")).realName;
        super.init();
        this.$scope.AgreementO = this.AgreementO;
        this.$scope.ContractO = this.ContractO;
        this.$scope.isContractO = true;
        this.$scope.isAgreementO = false;
        this.$scope.isAgreementF = false;
        this.$scope.isDownloadA = false;

        this.$scope.queryTerms = {
            clientName: "",
            contractNumber: "",
            carCodeBox: "",
            customerServiceId: "",
            origin: "",
            destination: "",
            deliveryBegin: "",
            deliveryEnd: "",
            econtractStatus: "",
            agreementEContractStatus: "",
            contractStartTime: "",
            contractEndTime: ""
        };

        this.$scope.gridOptions = {
            multiSelect: false,//单选
            enableRowSelection: true,
            enableRowHeaderSelection: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "序号", field: 'index', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '3%' },
                { displayName: "合同号", field: 'contractNumber', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "发货地址", field: 'origin', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '11%' },
                { displayName: '送货地址', field: 'destination', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '11%' },
                { displayName: '发货时间', field: 'deliveryTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '客服专员', field: 'customerName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '合同状态', field: 'eContractStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '5%' },
                { displayName: '补充协议状态', field: 'agreementEContractStatus', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '6%' },
                { displayName: '合同订立日期', field: 'contractTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                {
                    displayName: "操作",
                    field: 'eContractStatus',
                    cellTemplate:
                    '<a class="fa fa-download m-l-xs  primary text-success" ng-if="row.entity.eContractStatus == \'签署完成\'" title="下载" ng-click="grid.appScope.downloadContract(row.entity.carrierOrderId)" ></a>'
                    + '<a class="fa fa-eye m-l-xs  primary text-info" title="查看" ng-click="grid.appScope.showContractDetail(row.entity.electronicContractNumber,row.entity.agreementContractNumber)" ></a>',
                    enableColumnMenu: false, enableSorting: false, width: '5%'
                },
            ],
            onRegisterApi: (gridApi) => {

                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }

        }
        $('#openStartTime').click(function () {
            $('#deliveryBegin').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#deliveryEnd').datetimepicker('show');
        });
        $('#opencontractStartTime').click(function () {
            $('#contractStartTime').datetimepicker('show');
        });
        $('#opencontractEndTime').click(function () {
            $('#contractEndTime').datetimepicker('show');
        });


        this.query();
        this.$scope.showContractDetail = this.showContractDetail;
        this.$scope.downloadContract = this.downloadContract;
        this.$scope.downloadAgreement = this.downloadAgreement;
        this.$scope.skipDownload = this.skipDownload;
        this.loadCustomerServiceDropDown();
        this.loadContractStatusDropDown();


    }

    localHistory(): void {
        super.localHistory(this.$state)
    }
    getHistory(): void {
        super.getHistory(this.$state, this.$location)
    }


    /**
     * 加载数据
     */
    loadData(queryTerms: IEContractQueryTerms, skip: number, count: number): void {
        // if (this.queryTerms.econtractStatus == null || this.queryTerms.econtractStatus == undefined || this.queryTerms.econtractStatus == "") { this.queryTerms.econtractStatus = "1,2,3,4,5" };
        // if (this.queryTerms.agreementEContractStatus == null || this.queryTerms.agreementEContractStatus == undefined || this.queryTerms.agreementEContractStatus == "") { this.queryTerms.agreementEContractStatus = "1,2,3,4,5" };
        this.operationService.getEContractList(this.queryTerms.clientName, this.queryTerms.contractNumber, this.queryTerms.carCodeBox,
            this.queryTerms.customerServiceId, this.queryTerms.origin, this.queryTerms.destination, this.queryTerms.deliveryBegin, this.queryTerms.deliveryEnd, this.queryTerms.econtractStatus, this.queryTerms.agreementEContractStatus, this.queryTerms.contractStartTime, this.queryTerms.contractEndTime, skip, count).then((result) => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                result.data.data.map((item, index) => item.index = skip++ + 1);
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39) + 96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }


    /**
     * 客服专员下拉
     */
    loadCustomerServiceDropDown(): void {
        this.employeeService.getbytype("4", 0, -1).then(result => {//CommissionerType.CustomerService.toString()
            this.$scope.customerServiceDropDown = result.data.data;
        });
    }

    /**
     * 合同/补充协议状态下拉
     */
    loadContractStatusDropDown(): void {
        this.$scope.econtractStatusDropDown = this.valueService.getEContractStatus().data;
    }


    /**
     * 单选
     */
    selectChange = (row: uiGrid.IGridRowOf<EContractListResponse>) => {
        if (row.isSelected) {
            this.selectData = row.entity;
            if (this.selectData.agreementContractNumber != null && this.selectData.agreementContractNumber != "") {
                this.$scope.isAgreementF = true;
                this.agreementContractNumber = this.selectData.agreementContractNumber;
            } else {
                this.$scope.isAgreementF = false;
                if ($('.tab-pane:eq(1)').attr("class").search("active") != -1 && $('.uib-tab:eq(1)').attr("class").search("active") != -1) {
                    $('.tab-pane:eq(1)').removeClass("active");
                    $('.tab-pane:eq(0)').addClass("active");
                    $('.uib-tab:eq(1)').removeClass("active");
                    $('.uib-tab:eq(0)').addClass("active");
                }
            }
            if (this.selectData.agreementEContractStatus == "签署完成") {
                this.$scope.isDownloadA = true;
            }
            else {
                this.$scope.isDownloadA = false;
            }
            if (this.selectData.carrierOrderId != null) {
                this.carrierOrderId = this.selectData.carrierOrderId;
            }
            if (this.selectData.electronicContractNumber != null) {
                this.electronicContractNumber = this.selectData.electronicContractNumber;
            }

        } else {
            this.selectData = null;
        }


    }

    /**
     * 点击显示电子合同
     */
    ContractO = () => {
        if ($('.tab-pane:eq(1)').attr("class").search("active") != -1 && $('.uib-tab:eq(1)').attr("class").search("active") != -1) {
            $('.tab-pane:eq(1)').removeClass("active");
            $('.tab-pane:eq(0)').addClass("active");
            $('.uib-tab:eq(1)').removeClass("active");
            $('.uib-tab:eq(0)').addClass("active");
        }
        this.orderService.EContractDetail(this.electronicContractNumber).then((res) => {
            this.$scope.contractImgA = res.data.imgList[0];
            this.$scope.contractImgB = res.data.imgList[1];
            this.$scope.contractImgC = res.data.imgList[2];
        }, (rej) => { });
    }
    /**
     * 点击显示补充协议
     */
    AgreementO = () => {
        if ($('.tab-pane:eq(0)').attr("class").search("active") != -1 && $('.uib-tab:eq(0)').attr("class").search("active") != -1) {
            $('.tab-pane:eq(0)').removeClass("active");
            $('.tab-pane:eq(1)').addClass("active");
            $('.uib-tab:eq(0)').removeClass("active");
            $('.uib-tab:eq(1)').addClass("active");
        }
        this.orderService.EContractDetail(this.agreementContractNumber).then((res) => {
            this.$scope.agreementImgA = res.data.imgList[0];
            this.$scope.agreementImgB = res.data.imgList[1];
            this.$scope.agreementImgC = res.data.imgList[2];
            //let agreementContractNumber = this.selectData.agreementContractNumber;
        }, (rej) => { });
    }

    /**
   * 查看电子合同详情
   */
    showContractDetail = (electronicContractNumber: string, agreementContractNumber: string) => {

        if (electronicContractNumber != null) {
            this.orderService.EContractDetail(electronicContractNumber).then((res) => {
                this.$scope.contractImgA = res.data.imgList[0];
                this.$scope.contractImgB = res.data.imgList[1];
                this.$scope.contractImgC = res.data.imgList[2];

                $('#contract').modal('show');
            }, (rej) => { });
        }
        if (agreementContractNumber != null && agreementContractNumber != "") {
            this.orderService.EContractDetail(agreementContractNumber).then((res) => {
                this.$scope.agreementImgA = res.data.imgList[0];
                this.$scope.agreementImgB = res.data.imgList[1];
                this.$scope.agreementImgC = res.data.imgList[2];
            }, (rej) => { });
        }

    }

    /**
     * 下载电子合同
     */
    downloadContract = (carrierOrderId: string) => {
        if (carrierOrderId != null) {
            this.operationService.AddEContractDownloadRecord(carrierOrderId, "1", this.$scope.operatorName).then((res) => {

                window.location.href = res.data.extData;

            }, (rej) => { });
        }
    }

    /**
     * 下载补充协议
     */
    downloadAgreement = () => {
        if (this.carrierOrderId != null) {
            this.operationService.AddEContractDownloadRecord(this.carrierOrderId, "2", this.$scope.operatorName).then((res) => {

                window.location.href = res.data.extData;

            }, (rej) => { });
        }
    }

    /** 
      * 跳转下载记录
     */
    skipDownload = () => {
        this.$state.go("app.opera.operation_EContractDownload");
    }

}
angular.module("tms").controller("EContractListController", ["$scope", "valueService", "employeeService", "$ngBootbox", "$state", "routeService", "operationService", "$location", "orderService", IEContractListController]);
